module OrderHelper
  include UsersHelper
  include ProductsHelper
  include EnumsHelper::OrderStatus
  
  def get_current_order
    user_context = session[:user_context]
    if !user_context.order.nil?
      return user_context.order
    else
      return nil
    end
  end
  
  def create_order(product_id,quantity,comments)
    create_order_status = false
    product = Product.find(product_id)
    if (!product.nil?)      
      order = Order.new
      order.user = get_current_user()
      order.merchant = get_merchant_from_product(product_id)
      order.order_num = SecureRandom.uuid
      order_items_array = Array.new
      order.order_items = order_items_array
      order.order_status = EnumsHelper::OrderStatus::INIT
      order.amount = 0
      user_context = session[:user_context]
      user_context.order = order
      session['user_context'] = user_context
      create_order_status = add_item_to_order(product_id,quantity,comments)  
    else
      create_order_status = false       
    end
    return create_order_status
  end
  
  def add_item_to_order(product_id,quantity,comments)
    add_item_status = false
    user_context = session[:user_context]
    if user_context.order.nil?
      create_order(product_id,quantity,comments)
    else
      product = Product.find(product_id)
      if(!product.nil?)
        if(product.merchant.id == user_context.order.merchant.id)
          item_added = false
          user_context.order.order_items.each do |order_item|            
            if order_item.product.id == product.id
              order_item.quantity = order_item.quantity + quantity.to_i  
              order_item.total_amount = Float(product.price - (product.price * product.discount / 100)) * Float(order_item.quantity)
              item_added= true
              user_context.order.amount = user_context.order.amount + order_item.total_amount;
            end
          end
          if !item_added
            single_order_item = OrderItem.new
            single_order_item.item_num = SecureRandom.uuid
            single_order_item.product = product
            single_order_item.quantity = quantity
            single_order_item.comments = comments
            single_order_item.total_amount = Float(product.price - (product.price * product.discount / 100)) * Float(single_order_item.quantity)
            user_context.order.order_items.push(single_order_item)
            user_context.order.amount = user_context.order.amount + single_order_item.total_amount;
            session['user_context'] = user_context
            add_item_status = true
          end
        end
      end
    end
    return add_item_status
  end
  
  def remove_item_from_order(item_num)
    user_context = session[:user_context]
    i = 0
    user_context.order.order_items.each do |order_item|                  
      if order_item.item_num == item_num
         user_context.order.order_items.delete_at(i)
         user_context.order.amount = user_context.order.amount - order_item.total_amount;
         session['user_context'] = user_context
         return
      end
      i = i + 1
    end 
  end
  
  def get_item_from_order(item_num)
    user_context = session[:user_context]
    user_context.order.order_items.each do |order_item|                  
      if order_item.item_num == item_num
         return order_item
      end
    end 
  end
  def save_order()
    user_context = session[:user_context]
    user_context.order.order_status = EnumsHelper::OrderStatus::COMPLETE
    user_context.order.save
    user_context.order = nil
  end
  
  def edit_item_from_order(item_num,quantity,comments)
    user_context = session[:user_context]
    user_context.order.order_items.each do |order_item|                  
      if order_item.item_num == item_num
        order_item.quantity = quantity
        order_item.comments = comments
        user_context.order.amount = user_context.order.amount - order_item.total_amount;
        order_item.total_amount = Float(order_item.product.price - (order_item.product.price * order_item.product.discount / 100)) * Float(order_item.quantity)
        user_context.order.amount = user_context.order.amount + order_item.total_amount;
        session['user_context'] = user_context
        return
      end
    end 
  end

  def process_order(user_context, order_id, s_token, cc_masked)    
    @order = build_order(user_context, order_id)
    @res = make_purchase(userContext, order_id, s_token, cc_masked)
  end
  
  def build_order(user_context, order_id)
    order = Order.new      
    user = user_context.user
    product_id = user_context.get(order_id)    
    order.user = user
    product = Product.find(product_id)
    order.order_num=order_id
    order.order_status = EnumsHelper::OrderStatus::INIT
    order.merchant = product.merchant
    order_items_array = Array.new
    order.order_items = order_items_array
    order.amount = 0      
    user_context.order = order
    order.save                   
  end
  
end
